//
//  WXAddCarInfoTableViewController.m
//  CarJX
//
//  Created by King-ChenXu on 14-9-10.
//  Copyright (c) 2014年 KK. All rights reserved.
//

#import "WXAddCarInfoTableViewController.h"
#import <UIView+Toast.h>
#import "UIViewController+HttpRequest.h"

#import "WXInputSelectTableViewCell.h"
#import "WXSelectCarAreaTableViewController.h"
#import "WXQuetyBreakTableViewController.h"
@interface WXAddCarInfoTableViewController ()
@property (nonatomic,strong) NITableViewModel *model;
@property (nonatomic,weak) NIRadioGroupController *radioVC;
@property (nonatomic,strong)WXInputselectObject *CarArea;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) IBOutlet UIButton *addBtn;

@end



@implementation WXAddCarInfoTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
        
    }
    return self;
}

-(void) setup
{
    self.CarArea = [[WXInputselectObject alloc] init];
    self.CarArea.title = @"车牌归属";
    self.CarArea.placeholder = @"赣";
    self.CarArea.buttonTitle = @"点击选择";
    self.CarArea.target = self;
    self.CarArea.seletor = @selector(selectCarArea);
    
    self.carNo = [WXInputCellObject inputCellWithTitle:@"车牌号码" value:nil delegate:self];
    self.carNo.placeholder = @"车牌号";
    // self.carNo.inputTextLeftText = @"赣";
    
    self.carType = [[NIRadioGroup alloc] initWithController:self];
    self.carType.cellTitle = @"车辆类型";
    
    self.carType.delegate = self;
    
    [self.carType mapObject:[NITitleCellObject objectWithTitle:@"小型汽车"] toIdentifier:CarType2];
    [self.carType mapObject:[NITitleCellObject objectWithTitle:@"大型汽车"] toIdentifier:CarType1];
    [self.carType mapObject:[NITitleCellObject objectWithTitle:@"外籍汽车"] toIdentifier:CarType3];
    [self.carType mapObject:[NITitleCellObject objectWithTitle:@"农用汽车"] toIdentifier:CarType4];
    [self.carType mapObject:[NITitleCellObject objectWithTitle:@"外挂汽车"] toIdentifier:CarType5];
    [self.carType mapObject:[NITitleCellObject objectWithTitle:@"外籍汽车"] toIdentifier:CarType6];
    self.carType.selectedIdentifier = CarType2;
    
    self.zhengShuNo = [WXInputCellObject inputCellWithTitle:@"证书号" value:nil delegate:self];
    self.zhengShuNo.placeholder = @"机动车登记证书编号";
    
    self.jiaNo = [WXInputCellObject inputCellWithTitle:@"车架号" value:nil delegate:self];
    self.jiaNo.placeholder = @"请输入车架号后六位";
    
    self.engineNo = [WXInputCellObject inputCellWithTitle:@"发动机号码" value:nil delegate:self];
    self.engineNo.placeholder = @"请输入发动机号后六位";
    
    self.model = [[NITableViewModel alloc] initWithListArray:@[self.CarArea, self.carNo,self.carType,/*self.zhengShuNo ,*/self.jiaNo,self.engineNo ] delegate:(id)[NICellFactory class]];
    _imageView.hidden = YES;
    _addBtn.frame = CGRectMake(10, 20, 300, 30);
}



-(void)selectCarArea
{
    WXSelectCarAreaTableViewController *select = [[WXSelectCarAreaTableViewController alloc] init];
    [self.navigationController pushViewController:select animated:YES];
    __weak WXAddCarInfoTableViewController *self1 = self;
    select.callBack = ^(NSString *carArea){
        _CarArea.placeholder = carArea;
        //        if (![carArea isEqualToString:@"赣"]) {
        //            _imageView.hidden = YES;
        //            _model = [[NITableViewModel alloc] initWithListArray:@[self1.CarArea, self1.carNo,self1.carType /*, self.zhengShuNo*/ ,self1.jiaNo,self1.engineNo] delegate:(id)[NICellFactory class]];
        //            _addBtn.frame = CGRectMake(10, 20, 300, 30);
        
        //        } else{
        //            _model = [[NITableViewModel alloc] initWithListArray:@[self1.CarArea, self1.carNo,self1.carType , self1.zhengShuNo/*,self.jiaNo,self.engineNo*/ ] delegate:(id)[NICellFactory class]];
        //            _imageView.hidden = NO;
        //            _addBtn.frame = CGRectMake(10, 219, 300, 30);
        //        }
        self1.tableView.dataSource = self1.model;
        [self1.tableView reloadData];
        
    };
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    
    self.tableView.dataSource = self.model;
    self.tableView.delegate = [self.carType forwardingTo:self];
    [self setTitle:@"添加车辆"];
}


#pragma mark table delegate
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    if ([cell isKindOfClass:[WXInputTableViewCell class]]) {
        WXInputTableViewCell *inputCell = (WXInputTableViewCell *) cell;
        inputCell.titleLabel.textColor = [UIColor blackColor];
    }
    cell.backgroundColor = cell.contentView.backgroundColor = [UIColor whiteColor];
    
}
#pragma mark - 添加车辆
- (IBAction)addCarInfoAction:(UIButton *)sender {
    
    //car_num:车牌号
    //car_sn:车架号
    //car_eng:发动机号
    //car_zsh:证书号
    NSDictionary *carDic = nil;
    if (self.carNo.value.length <= 0) {
        [self.navigationController.view makeToast:@"请填写车牌号" duration:3 position:TOP_POINT];
        return;
    }
    NSString *str = [NSString stringWithFormat:@"%@%@",self.CarArea.placeholder,self.carNo.value];
    NSLog(@"%@",[[NSNumber numberWithInteger: self.carType.selectedIdentifier] stringValue]);
    NSString *cartype = [@"0" stringByAppendingString:[[NSNumber numberWithInteger: self.carType.selectedIdentifier] stringValue]];
    //    if (![self.CarArea.placeholder isEqualToString:@"赣"]) {
    
    if (self.jiaNo.value.length <= 0) {
        [self.navigationController.view makeToast:@"请填写车架号" duration:3 position:TOP_POINT];
        return;
    }
    if (self.engineNo.value.length <=0) {
        [self.navigationController.view makeToast:@"请填写发动机号" duration:3 position:TOP_POINT];
        return;
    }
    carDic = @{@"memberid" : [Common currentUserId], @"car_type":  cartype
               , @"car_num" : str , @"car_sn" : self.jiaNo.value , @"car_eng" : self.engineNo.value ,@"car_zsh" : @"" };
    //    }else{
    //        if (self.zhengShuNo.value.length<=0) {
    //            [self.navigationController.view makeToast:@"请填写证书号" duration:3 position:TOP_POINT];
    //            return;
    //        }
    //        carDic = @{@"memberid" : [WXUserStore userId], @"car_type":  cartype
    //                   , @"car_num" : str , @"car_sn" : @"" , @"car_eng" : @"" ,@"car_zsh" : self.zhengShuNo.value };
    //    }
    
    
    
    [self GETWithContentType:@"addcar" params:carDic success:^(ResultMy *result) {
        
        [self.navigationController.view makeToast:@"添加成功" duration:3 position:TOP_POINT];
        [self.navigationController popViewControllerAnimated:YES];
        
    } failure:^(NSError *error) {
        
        [self.navigationController.view makeToast:@"网络错误，请重试" duration:3 position:TOP_POINT];
        
    }];
    
}

#pragma mark radio
-(void)radioGroup:(NIRadioGroup *)radioGroup didSelectIdentifier:(NSInteger)identifier
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(BOOL)radioGroup:(NIRadioGroup *)radioGroup radioGroupController:(NIRadioGroupController *)radioGroupController willAppear:(BOOL)animated
{
    self.radioVC = radioGroupController;
    return YES;
}

-(NSString *)radioGroup:(NIRadioGroup *)radioGroup textForIdentifier:(NSInteger)identifier
{
    switch (identifier) {
        case CarType1:
            return @"大型汽车";
        case CarType2:
            return @"小型汽车";
        case CarType3:
            return @"外籍汽车";
        case CarType4:
            return @"农用汽车";
        case CarType5:
            return @"外挂汽车";
        case CarType6:
            return @"外籍汽车";
        default:
            break;
    }
    return nil;
}
@end
